package 二叉树相关问题;

import java.util.ArrayList;
import java.util.List;

public class 叶子相似的树 {

    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        traverse(root1,list1);
        traverse(root2,list2);
        if (list1.size()!=list2.size())
            return false;
        int sum=0;
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i)==list2.get(i))
                sum++;
        }
        return sum==list1.size();
    }

    private void traverse(TreeNode root, List<Integer> list) {
        if (root==null)
            return;
        traverse(root.left,list);
        if (root.left==null && root.right==null)
            list.add(root.val);
        traverse(root.right,list);
    }
}
